<?php

/* --|	SHARE
  Software: Socially
  Version: 1.0
  Contact: http://socially.googlecode.com
  Info: http://socially.googlecode.com
  Support: http://socially.googlecode.com
  --------------------------------------------------------------------------
  Author: goker.cebeci | http://gokercebeci.com
  --------------------------------------------------------------------------
  License: Distributed under the GNU General Public License (GPL)
  http://www.gnu.org/licenses/gpl.html
  This program is distributed in the hope that it will be useful - WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.
  ------------------------------------------------------------------------- */

class share {

    public static function post($a) {
        if (!$a['shareID'])
            $a['shareID'] = 1;
        if (!$a['senderID'])
            return common::result(800, _('Requered value is empty'));
        if (!$a['application'])
            return common::result(800, _('Requered value is empty'));
        if (!$a['itemID'] && !$a['title'])
            return common::result(400, _('Please enter a title'), array('title'));
        if (!$a['itemID'] && $a['title']) {
            profile::quickMessage(array('accountID' => $a['senderID'], 'message' => $a['title']));
        }
        $r = db::query(sprintf("INSERT INTO postieShare (`shareID`,`senderID`,`application`,`itemID`,`title`,`date`)
                    VALUES ('%d','%d','%s','%d','%s',NOW());",
                                $a['shareID'], $a['senderID'], $a['application'], $a['itemID'], $a['title']));
        return common::result(200, _('Your post has been sent.'));
    }

    public static function delete($a) {
        if ($a['sender'])
            db::query(sprintf("UPDATE postieAttendees SET status = 2 WHERE senderID = %d AND conversationID = %d;", $a['UID'], $a['CID']));
        else
            db::query(sprintf("UPDATE postieAttendees SET status = 2 WHERE recipientID = %d AND conversationID = %d;", $a['UID'], $a['CID']));
        return common::result(200, array(303, $a['UID'], 0, $a['CID']));
    }

    /* public static function read($a){
      db::query(sprintf("UPDATE postieAttendees SET status = 1 WHERE status = 0 AND conversationID = %d AND recipientID = %d;",$a['CID'],$a['UID']));
      $c = db::query(sprintf("SELECT c.ID, c.title, a.senderID,
      (SELECT `message` FROM postieMessages WHERE ID = a.messageID) as `message`,
      (SELECT `date` FROM postieMessages WHERE ID = a.messageID) as `date`
      FROM postieConversations as c, postieAttendees as a
      WHERE c.ID = a.conversationID  AND c.ID = %d AND (senderID = %d OR recipientID = %d)  ORDER BY a.`ID` DESC LIMIT 20;",$a['CID'],$a['UID'],$a['UID']),true,true);
      if($c) foreach ($c as $k=>$i){
      $c[$k]->sender = profile::get(array('ID'=>$i->senderID));
      }
      return $c;
      } */

    public static function thread($a) {
        $t = db::query(sprintf("
        SELECT * FROM postieShare WHERE senderID IN
        ( SELECT IF(memberOne='%d',memberTwo,memberOne) as ID FROM friends WHERE 1=1 AND (memberOne = %d OR memberTwo = %d) AND status = 'friend' ) 
        ORDER BY `date` DESC LIMIT 30;", $a['accountID'], $a['accountID'], $a['accountID']), true, true);
        if ($t)
            foreach ($t as $k => $i) {
                $t[$k]->sender = profile::get(array('ID' => $i->senderID));
            }
        return $t;
    }

    public static function attendees($a) {
        $r = db::query(sprintf("SELECT recipientID as RID FROM postieAttendees WHERE conversationID = %d AND recipientID <> %d GROUP BY recipientID;", $a['CID'], $a['UID']), true, true);
        return (count($r)) ? $r : db::query(sprintf("SELECT senderID as RID FROM postieAttendees WHERE conversationID = %d ORDER BY ID ASC LIMIT 1;", $a['CID']), true, true);
    }

    /* public static function unread($a){
      return db::query(sprintf("SELECT COUNT(ID) as unread FROM postieAttendees  WHERE recipientID = %d AND status = 0;",$a),true);
      } */
}

?>